<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>添加商品</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-alert
                    title="添加商品信息"
                    :closable="false"
                    center
                    show-icon
                    type="info"
            >
            </el-alert>
            <el-steps
                    :space="200"
                    :active="activeIndex - 0"
                    finish-status="success"
                    align-center
            >
                <el-step title="基本信息"></el-step>
                <el-step title="商品图片"></el-step>
                <el-step title="商品简介"></el-step>
                <el-step title="完成"></el-step>
            </el-steps>

            <el-form
                    ref="addFormRef"
                    :model="addForm"
                    :rules="addFormRules"
                    label-width="80px"
                    label-position="top"
            >
                <el-tabs
                        v-model="activeIndex"
                        :tab-position="'left'"
                >
                    <el-tab-pane label="基本信息" name="0">
                        <el-form-item label="商品名称" prop="cname">
                            <el-input v-model="addForm.goods_name"></el-input>
                        </el-form-item>
                        <el-form-item label="商品价格" prop="price">
                            <el-input v-model="addForm.goods_price"  min="0"  type="number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品数量" prop="count">
                            <el-input v-model="addForm.goods_number" min="0" type="number"></el-input>
                        </el-form-item>
                        <el-form-item label="商品类型：" prop="model">
                            <el-select style="width: 100%;" v-model="addForm.goods_cat" placeholder="请选择">
                                <el-option
                                        v-for="(item ,index) in catelist"
                                        :key="index"
                                        :label="item.type"
                                        :value="item.type">
                                </el-option>
                            </el-select>
<!--                            <el-input v-model="addForm.goods_cat"></el-input>-->
                        </el-form-item>
                    </el-tab-pane>

                    <el-tab-pane label="商品图片" name="1">
                        <el-upload
                                action="http://118.25.132.164:8080/api/permit/upload/avatar"
                                accept="image/*"
                                name="myfile"
                                :limit="1"
                                :on-remove="handleRemove"
                                :on-success="handleSuccess"
                                list-type="picture"
                        >
                            <el-button size="small" type="primary">点击上传</el-button>
                        </el-upload>
                    </el-tab-pane>
                    <el-tab-pane label="商品简介" name="2">
                        <el-input v-model="addForm.goods_introduce" type="textarea "></el-input>
<!--                        <quill-editor v-model="addForm.goods_introduce"></quill-editor>-->
                        <el-button type="primary" class="btnAdd" @click="add">添加商品</el-button>
                    </el-tab-pane>
                </el-tabs>
            </el-form>
        </el-card>

        <!--<el-dialog
          title="图片预览"
          :visible.sync="previewVisible"
          width="50%">
          <img :src="previewPath" alt="" class="previewImg">
        </el-dialog>-->
    </div>
</template>

<script>
  //import _ from 'lodash'

  export default {
    data() {
      return {
        activeIndex: "0",
        addForm: {
          goods_name: "",
          goods_price: 0,
          goods_number: 0,
          goods_cat: "",
          pics: "",
          goods_introduce: '',
        },
        addFormRules: {
          goods_name: [
            {required: true, message: "请输入商品名称", trigger: "blur"}
          ],
          goods_price: [
            {required: true, message: "请输入商品价格", trigger: "blur"}
          ],
          goods_number: [
            {required: true, message: "请输入商品数量", trigger: "blur"}
          ],
          goods_cat: [
            {required: true, message: "请输入商品类型", trigger: "blur"}
          ]
        },
        headerobj: {
          Authorization: window.sessionStorage.getItem('token')
        },
        catelist: []
        //previewPath:'',
        //previewVisible: false
      };
    },
    methods: {
      handlePreview(file) {
        console.log(file)
        //this.previewPath = file.response.data.url
        this.previewVisible = true
      },
      handleRemove() {
        this.addForm.pics = ""
      },
      handleSuccess(response) {
        console.log(response);
        this.addForm.pics = response.data.url
      },
      async getAllType() {
        let queryInfo={
          query: "",
            pagenum: 1,
            pagesize: 100
        }
        const {data} = await this.$http.get("mtype/findTypeByParams", {
          params: queryInfo
        });
        this.catelist = data.data.content;
      },
      add() {
        this.$refs.addFormRef.validate(async valid => {
          if (!valid) {
            return this.$message.error('请先填写必填项！')
          }
          let params = {
            cname: this.addForm.goods_name,
            count: this.addForm.goods_number,
            detail: this.addForm.goods_introduce,
            fashion: null,
            imgUrl: this.addForm.pics,
            model: this.addForm.goods_cat,
            price: this.addForm.goods_price,
            quality: null,
            star: 0,
          }
          /* const form = _.cloneDeep(this.addForm)
           form.goods_cat = form.goods_cat.join(',')
           this.manyTabelData.forEach(item => {
             const newInfo = {
               attr_id:item.attr_id,
               attr_value:item.attr_vals.join(' ')
             }
             this.addForm.attrs.push(newInfo)
           });
           this.onlyTabelData.forEach(item => {
             const newInfo = {
               attr_id:item.attr_id,
               attr_value:item.attr_vals
             }
             this.addForm.attrs.push(newInfo)
           });
           form.attrs = this.addForm.attrs*/
          const {data} = await this.$http.post('commodity/insert',params)
          console.log(data)
          if (data.meta.status == 201) {
            return this.$message.error(data.meta.msg);
          }
          this.$message.success('添加商品成功！')



          this.$router.push('/goods')
        })
      }
    },
    created() {
      this.getAllType()
      //this.getCateList();
    },
  };
</script>

<style lang="less" scoped>
    .el-checkbox {
        margin: 0 10px 0 0 !important;
    }

    .previewImg {
        width: 100%;
    }

    .btnAdd {
        margin-top: 15px;
    }
</style>